package com.julang.check.bean.pay;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

import com.julang.check.bean.CommonEntity.*;

import static com.julang.check.utils.CommonData.*;
/**
 * @author 赵自强
 * */
public class PayData {
    private static List<OrderDetail> unUsedOrder = new LinkedList<>();
    private static List<OrderDetail> finishedOrder = new LinkedList<>();

    public static float getMoneyByOrderId(String orderId) {
        for (Iterator<OrderDetail> iterator = unUsedOrder.iterator(); iterator.hasNext(); ) {
            OrderDetail orderDetail = iterator.next();
            if (orderDetail.getOutTradeNo().equals(orderId)) {
                if (orderDetail.getPlatformType() == 0) {
                    return Float.valueOf(orderDetail.getTotalFee());
                }else {
                    return Float.valueOf(orderDetail.getTotalFee()) / 100;
                }
            }
        }
        return -1;
    }
    public static String getUserIdByOrderId(String orderId){
        for (Iterator<OrderDetail> iterator = unUsedOrder.iterator(); iterator.hasNext(); ) {
            OrderDetail orderDetail = iterator.next();
            if (orderDetail.getOutTradeNo().equals(orderId)) {
                return orderDetail.getUserId();
            }
        }
        return null;
    }
    public static List<OrderDetail> getOrderByUserId(String userId) {
        List<OrderDetail> list = new ArrayList<>();
        for (Iterator<OrderDetail> iterator = unUsedOrder.iterator(); iterator.hasNext(); ) {
            OrderDetail orderDetail = iterator.next();
            if (orderDetail.getUserId().equals(userId)) {
                list.add(orderDetail);
            }
        }
        for (Iterator<OrderDetail> iterator = finishedOrder.iterator(); iterator.hasNext(); ) {
            OrderDetail orderDetail = iterator.next();
            if (orderDetail.getUserId().equals(userId)) {
                list.add(orderDetail);
            }
        }
        return list;
    }
    public static boolean haveUnusedOrderByUserId(String userId) {
        for (Iterator<OrderDetail> iterator = unUsedOrder.iterator(); iterator.hasNext(); ) {
            OrderDetail orderDetail = iterator.next();
            if (orderDetail.getUserId().equals(userId)) {
                return true;
            }
        }
        return false;
    }
    public static List<OrderDetail> getUnUsedOrder() {
        return unUsedOrder;
    }

    public static List<OrderDetail> getFinsheddOrder() {
        return finishedOrder;
    }

    public static boolean makeOrderFinished(String orderId, int tradeState, String platformTransactionId, String planformUserId) {
        if (tradeState != ONEOFNUMBER && tradeState != TWOOFNUMBER) {
            return false;
        }
        for (Iterator<OrderDetail> iterator = unUsedOrder.iterator(); iterator.hasNext(); ) {
            OrderDetail orderDetail = iterator.next();
            if (orderDetail.getOutTradeNo().equals(orderId)) {
                orderDetail.setPayTime(System.currentTimeMillis());
                orderDetail.setTradeState(tradeState);
                orderDetail.setPlatformTransactionId(platformTransactionId);
                orderDetail.setPlatformUserOpenid(planformUserId);
                finishedOrder.add(orderDetail);
                iterator.remove();
                return true;
            }
        }
        return false;
    }

    ;

    public static void createNewOrder(String userId, String outTradeNo, String totalFee, String qrCode, int platform) {
        OrderDetail orderDetail = new OrderDetail();
        orderDetail.setCreateTime(System.currentTimeMillis());
        orderDetail.setQrCode(qrCode);
        orderDetail.setTotalFee(totalFee);
        orderDetail.setUserId(userId);
        orderDetail.setOutTradeNo(outTradeNo);
        orderDetail.setTradeState(0);
        orderDetail.setPlatformType(platform);
        unUsedOrder.add(orderDetail);
    }
}
